package com.atguigu.service.impl;

import com.atguigu.dao.BookDao;
import com.atguigu.dao.impl.BookDaoImpl;
import com.atguigu.pojo.Book;
import com.atguigu.pojo.Page;
import com.atguigu.service.BookService;

import java.util.List;

public class BookServiceImpl implements BookService {

    private BookDao bookDao = new BookDaoImpl();
    @Override
    public void addBook(Book book) {
        bookDao.addBook(book);
    }

    @Override
    public void deleteBookById(Integer id) {
        bookDao.deleteBookById(id);
    }

    @Override
    public void updateBook(Book book) {
        bookDao.updateBook(book);
    }

    @Override
    public Book queryBookById(Integer id) {
       Book book =  bookDao.queryBookById(id);
        return book;
    }

    @Override
    public List<Book> queryBooks() {
        List<Book> bookList = bookDao.queryBooks();
        return bookList;
    }


    @Override
    public Page<Book> page(int pageNo, int pageSize) {
        Page<Book> page = new Page<>();
        Integer pageTotalCount = bookDao.queryForPageTotalCount();


        //2.设置每页显示的数量
        page.setPageSize(pageSize);

        //3.设置总记录数
        page.setPageTotalCount(pageTotalCount);

        //4.设置总页码
        Integer pageTotal = pageTotalCount / pageSize;
        if(pageTotalCount % pageSize >0){
            pageTotal++;
        }
        page.setPageTotal(pageTotal);

        //1.页数
        page.setPageNo(pageNo);


        int begin = (page.getPageNo()-1)*pageSize;
        //求当前页数据
        List<Book> items = bookDao.queryForPageItems(begin, pageSize);
        //设置当前页数据
        page.setItems(items);



        return page;
    }

    @Override
    public Page<Book> pageByPrice(int pageNo, int pageSize, int min, int max) {
        Page<Book> page = new Page<>();

        Integer pageTotalCount = bookDao.queryForPageTotalCountByPrice(min,max);


        //2.设置每页显示的数量
        page.setPageSize(pageSize);

        //3.设置总记录数
        page.setPageTotalCount(pageTotalCount);

        //4.设置总页码
        Integer pageTotal = pageTotalCount / pageSize;
        if(pageTotalCount % pageSize >0){
            pageTotal++;
        }
        page.setPageTotal(pageTotal);

        //1.页数
        page.setPageNo(pageNo);


        int begin = (page.getPageNo()-1)*pageSize;
        //求当前页数据
        List<Book> items = bookDao.queryForPageItemsByPrice(begin, pageSize,min,max);
        //设置当前页数据
        page.setItems(items);



        return page;
    }
}
